﻿using System;
using System.Reflection;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Ewk.Runtime.Serialization.PositionBased;
using Ewk.Runtime.Serialization.UnitTests.Records.PositionBased;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Ewk.Runtime.Serialization.UnitTests.PositionBased
{
    /// <summary>
    /// Summary description for PositionBasedDatafieldAttribute_DateTimeTests
    /// </summary>
    [TestClass]
    public class PositionBasedDatafieldAttribute_DateTimeTests
    {
        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void PositionBasedDatafieldAttribute_can_deserialize_a_valid_empty_DateTime_string_with_aberrant_DefaultCharacter_to_a_DateTime_instance()
        {
            var fieldAttribute = new PositionBasedDatafieldAttribute(0, 10) { DefaultCharacter = 'X', Alignment = DatafieldAlignment.Left };
            fieldAttribute.PropertyInfo = GetPropertyInfo();

            var result = fieldAttribute.Deserialize("XXXXXXXXXX");

            Assert.IsInstanceOfType(result, typeof(DateTime));

            var typedResult = (DateTime)result;
            Assert.AreEqual(default(DateTime), typedResult);
        }

        static PropertyInfo GetPropertyInfo()
        {
            return typeof(PositionBasedTestDatarecordWithDateTimeField).GetProperty("Field");
        }

        static PropertyInfo GetPropertyInfo(PositionBasedTestDatarecordWithDateTimeField record)
        {
            return record.GetType().GetProperty("Field");
        }

    }
}
